package org.shan.security.oaresource.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;

/**
 * Created by amanda.shan on 2019/3/12.
 */
@Configuration
@EnableResourceServer
public class MyResourcesServerConfig extends ResourceServerConfigurerAdapter {

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    //与授权服务器使用共同的密钥进行解析
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("123");
        return converter;
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {

//        resources.authenticationEntryPoint(new CustomAuthenticationEntryPoint());
//        resources
//                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/user/login"))
//                .stateless(false)
//
//        ;
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        super.configure(http);


        http
                .securityContext()
                .securityContextRepository(new HttpSessionSecurityContextRepository())
                .and()
            // 定义表单登录 - 身份认证的方式
            .formLogin()
            .loginPage("/user/login")
            // 处理登录请求路径
            .loginProcessingUrl("/authentication/form")
            .and()
            .authorizeRequests()
                .antMatchers("/user").anonymous()
                .antMatchers("/user/login", "/authentication/form", "/code/**").permitAll()
                .anyRequest().authenticated()
            .and()
            .requestMatchers()
                .anyRequest()
            .and()
            .anonymous().authorities("ROLE_ANON")
            .and()
            .csrf().disable();  // csrf 在后面章节会讲解
        ;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence rawPassword) {
                return rawPassword.toString();
            }

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
                return rawPassword.equals(encodedPassword);
            }
        };
    }
}
